package client;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.HeadlessException;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.Font;
import javax.swing.JTable;
import javax.swing.JLabel;
import javax.swing.table.DefaultTableModel;
import javax.swing.JScrollPane;

import server.BkCAccITF;
import server.BkFAccITF;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.Calendar;
import java.util.Date;

import client.Configuration;


public class BkFixedAccountGUI extends JFrame {
	
	private JTable table;
	Person ps = new Person();
	int pID = ps.getPID();
	
	private Object[][] FAcc;
	/**
	 * Launch the application.
	 */
	
	Configuration config=new Configuration();
	String rmiAddress=config.getRmiAddress();
	BkFAccITF FAitf;
	
	/*public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BkFixedAccountGUI frame = new BkFixedAccountGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}*/

	/**
	 * Create the frame.
	 */
	public BkFixedAccountGUI() {
		setTitle("\u5B9A\u671F\u8D26\u6237");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(null);
		
		JButton button = new JButton("\u53D6\u6B3E");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					FAitf = (BkFAccITF) Naming.lookup(rmiAddress+"BankFixedAccount");
					int a[]=table.getSelectedRows();
					int FANum=(Integer)table.getValueAt(a[0], 0);
					FAitf.withdrawl(pID, FANum);
					dispose();
						JOptionPane.showMessageDialog(null, "取款成功，已转入您的活期账户。");
						BkFixedAccountGUI frame = new BkFixedAccountGUI();
						frame.setLocationRelativeTo(null);
						frame.setVisible(true);
						
						
				} catch (MalformedURLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (NotBoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			
			}
		});
		button.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		button.setBounds(278, 178, 97, 39);
		getContentPane().add(button);
		
		JLabel label = new JLabel("\u5DF2\u6709\u5B58\u6B3E\u9879\uFF1A");
		label.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		label.setBounds(30, 26, 97, 18);
		getContentPane().add(label);
		
		JButton button_2 = new JButton("\u65B0\u5EFA\u5B58\u6B3E");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				BkAddFAccGUI frame = new BkAddFAccGUI();
				frame.setLocationRelativeTo(null);
				frame.setVisible(true);
				dispose();
			}
		});
		button_2.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		button_2.setBounds(64, 178, 97, 39);
		getContentPane().add(button_2);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(40, 62, 367, 106);
		getContentPane().add(scrollPane);
		
		
		
		try {
			FAitf = (BkFAccITF) Naming.lookup(rmiAddress+"BankFixedAccount");
			int j=FAitf.getpSize(pID);
			FAcc=new Object[j][];//////得到行
			
			int faNum=FAitf.getFirstFANum(pID);
			int i=0;
			while(faNum!=-1){
			FAcc[i]=new Object[4];
			FAcc[i][0]=new Integer(faNum);
			FAcc[i][1]=new Double(FAitf.getFASum(pID,faNum));
			FAcc[i][2]=FAitf.getStartTime(pID,faNum);
			FAcc[i][3]=new Integer(FAitf.getLimit(pID,faNum));
			i++;
			faNum=FAitf.getNextFANum(pID, faNum);
		}
		} catch (MalformedURLException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		} catch (RemoteException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		} catch (NotBoundException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		
		table = new JTable();
	
		table.setModel(new DefaultTableModel(
			
			FAcc,
			new String[] {
				"\u5B58\u6B3E\u53F7", "\u5B58\u6B3E\u989D", "\u8D77\u59CB\u65F6\u95F4", "\u5E74\u9650"
			}
		));
		scrollPane.setViewportView(table);
		
		JButton button_3 = new JButton("\u67E5\u770B\u5229\u606F");
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				try {
					FAitf = (BkFAccITF) Naming.lookup(rmiAddress+"BankFixedAccount");
					int a[]=table.getSelectedRows();
					int FANum=(Integer)table.getValueAt(a[0], 0);
					
					JOptionPane.showMessageDialog(null, FAitf.checkInterest(pID, FANum));
				} catch (HeadlessException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (MalformedURLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (NotBoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		button_3.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		button_3.setBounds(171, 178, 97, 39);
		getContentPane().add(button_3);
		
		JButton button_1 = new JButton("\u8FD4\u56DE");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				BankAccountGUI frame = new BankAccountGUI();
				frame.setLocationRelativeTo(null);
				frame.setVisible(true);
				dispose();
			}
		});
		button_1.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		button_1.setBounds(341, 10, 83, 22);
		getContentPane().add(button_1);
		
		JLabel lblNewLabel = new JLabel("注意：未到期取款将不获利息。");
		lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		lblNewLabel.setBounds(216, 227, 191, 25);
		getContentPane().add(lblNewLabel);
		

	}
}
